using System;
using System.Collections;
using System.Collections.Generic;
using Server.Mobiles;
using Server.Items;
using Server.Gumps;
using Server.Network;


namespace Server.Gumps
{
	public class AddTradeItemGump : Gump
	{
		public static readonly int GumpOffsetX = PropsConfig.GumpOffsetX;
		public static readonly int GumpOffsetY = PropsConfig.GumpOffsetY;

		public static readonly int TextHue = PropsConfig.TextHue;
		public static readonly int TextOffsetX = PropsConfig.TextOffsetX;

		public static readonly int OffsetGumpID = PropsConfig.OffsetGumpID;
		public static readonly int 	EntryGumpID = PropsConfig.EntryGumpID;
		public static readonly int   BackGumpID = PropsConfig.BackGumpID;
		public static readonly int    SetGumpID = PropsConfig.SetGumpID;

		public static readonly int SetWidth = PropsConfig.SetWidth;
		public static readonly int SetOffsetX = PropsConfig.SetOffsetX, SetOffsetY = PropsConfig.SetOffsetY;
		public static readonly int SetButtonID1 = PropsConfig.SetButtonID1;
		public static readonly int SetButtonID2 = PropsConfig.SetButtonID2;

		public static readonly int OffsetSize = PropsConfig.OffsetSize;

		public static readonly int EntryHeight = PropsConfig.EntryHeight;
		public static readonly int BorderSize = PropsConfig.BorderSize;

		private static readonly int HeaderTextHue = 777;

		private static readonly int NameWidth = 107;
		private static readonly int ValueWidth = 128;
		private static readonly int EntryCount = 2;

		private static readonly int TotalWidth = OffsetSize + NameWidth + OffsetSize + ValueWidth + OffsetSize + SetWidth + OffsetSize;
		private static readonly int TotalHeight = OffsetSize + ((EntryHeight + OffsetSize) * (EntryCount + 1));

		private static readonly int BackWidth = BorderSize + TotalWidth + BorderSize;

		private Mobile m_vendor;
		private Item m_item;
		private int m_qty;

		private int m_btOffset = 12000;

		public AddTradeItemGump( Mobile vendor, Item item ) : base( GumpOffsetX, GumpOffsetY )
		{
			m_vendor = vendor;
			m_item = item;

			IHardsellable hs = vendor as IHardsellable;
			TradeItem ti = VendorManager.GetTradeItem( hs, item );

			if ( ti == null )
				return;

			m_qty = 20;

			if ( vendor is RestockableVendor )
			{
				RestockableVendor rv = vendor as RestockableVendor;

				for ( int i = 0; i < rv.ExternalBuyInfo.Count; i++ )
				{
					Type type = item.GetType();

					if ( item is CloneMobile )
					{
						BaseCreature bc = ((CloneMobile)item).ClonePet;
						type = bc.GetType();
					}

					GenericBuyInfo gbi = (GenericBuyInfo)rv.ExternalBuyInfo[i];

					if ( gbi.Type == type && gbi.Hue == item.Hue )
						m_qty = gbi.MaxAmount;
				}
			}

			if ( hs == null || ti == null )
				return;

			string name = "";
			string sval = "";

			int entryCount = EntryCount;
			if ( vendor is RestockableVendor )
				entryCount++;

			int totalHeight = OffsetSize + ( ( EntryHeight + OffsetSize ) * ( entryCount + 1 ) );

			AddPage( 0 );

			AddBackground( 0, 0, BackWidth, BorderSize + totalHeight + BorderSize, BackGumpID );
			AddImageTiled( BorderSize, BorderSize, TotalWidth, totalHeight, OffsetGumpID );

			int x = BorderSize + OffsetSize;
			int y = BorderSize + OffsetSize;

			AddLabelCropped( x + TextOffsetX, y, NameWidth - TextOffsetX, EntryHeight, HeaderTextHue, "Item Setup" );

			for ( int i = 0; i < entryCount; i++ )
			{
				x = BorderSize + OffsetSize;
				y += EntryHeight + OffsetSize;

				switch ( i )
				{
					case 0:
					{
						name = "Description";
						if ( ti.Description != null && ti.Description.Length > 0 )
							sval = ti.Description;
						else
							sval = VendorManager.ToGetName( item, false );	// = ( 1020000 + ( item.ItemID & 0x3FFF ) ).ToString()

						break;
					}
					case 1:
					{
						name = "Price";
						sval = ti.Price.ToString();

						break;
					}
					case 2: name = "Quantity"; sval = m_qty.ToString(); break;
				}

				AddImageTiled( x, y, NameWidth, EntryHeight, EntryGumpID );
				AddLabelCropped( x + TextOffsetX, y, NameWidth - TextOffsetX, EntryHeight, TextHue, name );
				x += NameWidth + OffsetSize;
				AddImageTiled( x, y, ValueWidth, EntryHeight, EntryGumpID );
				AddLabelCropped( x + TextOffsetX, y, ValueWidth - TextOffsetX, EntryHeight, TextHue, sval );
				x += ValueWidth + OffsetSize;

				if ( SetGumpID != 0 )
					AddImageTiled( x, y, SetWidth, EntryHeight, SetGumpID );

				AddButton( x + SetOffsetX, y + SetOffsetY, SetButtonID1, SetButtonID2, i + m_btOffset, GumpButtonType.Reply, 0 );
			}
		}

		public override void OnResponse( NetState state, RelayInfo info )
		{
			int adjusted = 0;

			if ( info.ButtonID > 0 )
				adjusted = info.ButtonID - m_btOffset + 1;

			Mobile from = state.Mobile;

			switch ( adjusted )
			{
				case 0: // Closed
				{

					break;
				}
				default:
				{
					from.SendGump( new TradeItemTextEntryGump( m_vendor, m_item, m_qty, adjusted ) );

					break;
				}
			}
		}
	}

	public class TradeItemTextEntryGump : Gump
	{
		public static readonly int GumpOffsetX = PropsConfig.GumpOffsetX;
		public static readonly int GumpOffsetY = PropsConfig.GumpOffsetY;

		public static readonly int TextHue = PropsConfig.TextHue;
		public static readonly int TextOffsetX = PropsConfig.TextOffsetX;

		public static readonly int OffsetGumpID = PropsConfig.OffsetGumpID;
		public static readonly int HeaderGumpID = PropsConfig.HeaderGumpID;
		public static readonly int  EntryGumpID = PropsConfig.EntryGumpID;
		public static readonly int   BackGumpID = PropsConfig.BackGumpID;
		public static readonly int    SetGumpID = PropsConfig.SetGumpID;

		public static readonly int SetWidth = PropsConfig.SetWidth;
		public static readonly int SetOffsetX = PropsConfig.SetOffsetX, SetOffsetY = PropsConfig.SetOffsetY;
		public static readonly int SetButtonID1 = PropsConfig.SetButtonID1;
		public static readonly int SetButtonID2 = PropsConfig.SetButtonID2;

		public static readonly int PrevWidth = PropsConfig.PrevWidth;
		public static readonly int NextWidth = PropsConfig.NextWidth;

		public static readonly int OffsetSize = PropsConfig.OffsetSize;

		public static readonly int EntryHeight = PropsConfig.EntryHeight;
		public static readonly int BorderSize = PropsConfig.BorderSize;

		private static readonly int EntryWidth = 212;

		private static readonly int TotalWidth = OffsetSize + EntryWidth + OffsetSize + SetWidth + OffsetSize;
		private static readonly int TotalHeight = OffsetSize + (2 * (EntryHeight + OffsetSize));

		private static readonly int BackWidth = BorderSize + TotalWidth + BorderSize;
		private static readonly int BackHeight = BorderSize + TotalHeight + BorderSize;

		private static string[] m_Names = new string[]{ "True", "False" };

		private Mobile m_vendor;
		private Item m_item;
		private int m_qty;

		private int m_btOffset = 12000;

		public TradeItemTextEntryGump( Mobile vendor, Item item, int qty, int button ) : base( GumpOffsetX, GumpOffsetY )
		{
			m_vendor = vendor;
			m_item = item;
			m_qty = qty;

			int bt = button;

			IHardsellable hs = vendor as IHardsellable;
			TradeItem ti = VendorManager.GetTradeItem( hs, item );

			if ( hs == null || ti == null )
				return;

			string title = "";
			string initialText = "";

			if ( bt == 1 )
			{
				title = "Description";
				if ( ti.Description != null && ti.Description.Length > 0 )
					initialText = ti.Description;
			}
			else if ( bt == 2 )
			{
				title = "Price";
				initialText = ti.Price.ToString();
			}
			else if ( bt == 3 )
			{
				title = "Quantity";
				initialText = m_qty.ToString();
			}


			AddPage( 0 );

			AddBackground( 0, 0, BackWidth, BackHeight, BackGumpID );
			AddImageTiled( BorderSize, BorderSize, TotalWidth, TotalHeight, OffsetGumpID );

			int x = BorderSize + OffsetSize;
			int y = BorderSize + OffsetSize;

			AddImageTiled( x, y, EntryWidth, EntryHeight, EntryGumpID );
			AddLabelCropped( x + TextOffsetX, y, EntryWidth - TextOffsetX, EntryHeight, TextHue, title );
			x += EntryWidth + OffsetSize;

			if ( SetGumpID != 0 )
				AddImageTiled( x, y, SetWidth, EntryHeight, SetGumpID );

			x = BorderSize + OffsetSize;
			y += EntryHeight + OffsetSize;

			AddImageTiled( x, y, EntryWidth, EntryHeight, EntryGumpID );
			AddTextEntry( x + TextOffsetX, y, EntryWidth - TextOffsetX, EntryHeight, TextHue, 0, initialText );
			x += EntryWidth + OffsetSize;

			if ( SetGumpID != 0 )
				AddImageTiled( x, y, SetWidth, EntryHeight, SetGumpID );

			AddButton( x + SetOffsetX, y + SetOffsetY, SetButtonID1, SetButtonID2, bt + m_btOffset, GumpButtonType.Reply, 0 );
		}

		public override void OnResponse( NetState state, RelayInfo info )
		{
			Mobile from = state.Mobile;
			int adjusted = 0;

			if ( info.ButtonID > 0 )
				adjusted = info.ButtonID - m_btOffset;

			IHardsellable hs = m_vendor as IHardsellable;
			TradeItem ti = VendorManager.GetTradeItem( hs, m_item );

			if ( hs == null || ti == null )
			{
				from.SendMessage( "Error: vendor or item null." );
				return;
			}

			switch ( adjusted )
			{
				case 0: // Closed
				{


					break;
				}
				case 1:
				{
					TextRelay text = info.GetTextEntry( 0 );
					ti.Description = text.Text;

					if ( m_vendor is RestockableVendor )
						VendorManager.UpdateRestockable( from, ti, -1 );

					from.SendGump( new AddTradeItemGump( m_vendor, m_item ) );
					break;
				}
				case 2: // Price
				{
					TextRelay text = info.GetTextEntry( 0 );

					int value;
					bool parsed = Int32.TryParse( text.Text, out value );

					if ( parsed )
					{
						value = ( value > 100000000 ? 100000000 : value );
						ti.Price = value;

						if ( m_vendor is RestockableVendor )
							VendorManager.UpdateRestockable( from, ti, -1 );
					}

					from.SendGump( new AddTradeItemGump( m_vendor, m_item ) );
					break;
				}
				case 3: // Quantity
				{
					TextRelay text = info.GetTextEntry( 0 );

					int value;
					bool parsed = Int32.TryParse( text.Text, out value );

					if ( parsed )
					{
						int max = m_item.Stackable ? RestockableVendor.MaxStackable : RestockableVendor.MaxQuantity;

						value = ( value > max ? max : value );

					if ( m_vendor is RestockableVendor && value > 0 )
						VendorManager.UpdateRestockable( from, ti, value );
					}

					from.SendGump( new AddTradeItemGump( m_vendor, m_item ) );
					break;
				}
			}
		}
	}
}
